package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import tech.aistar.entity.User;
import tech.aistar.service.UserService;

import java.util.List;

/**
 * 本类用来演示:
 *
 * @author jyz
 * @date 2023/3/4 15:17:33
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //通过id得到用户信息
    @RequestMapping(value = "/getUser/{id}")
    public String getUser(@PathVariable int id){
        return userService.getUser(id).toString();
    }

    //通过id删除用户信息
    @RequestMapping(value = "/delete")
    public String delete(int id){
        int result = userService.delete(id);
        if(result >= 1){
            return "删除成功！";
        }else{
            return "删除失败！";
        }
    }

    //更改用户信息
    @RequestMapping(value = "/update")
    public String update(User user){
        int result = userService.update(user);
        if(result >= 1){
            return "更新成功！";
        }else{
            return "更新失败！";
        }
    }

    //插入用户信息
    @RequestMapping(value = "/insert")
    public int insert(User user){
        return userService.save(user);
    }

    //查询所有用户的信息
    @RequestMapping(value = "/selectAll")
    @ResponseBody   //理解为：单独作为响应体，这里不调用实体类的toString方法
    public List<User> listUser(){
        return userService.findAll();
    }
}
